#include <iostream>
#include <fstream> 
#include "student.h" 
using namespace std;

int main() {
	student s[20];
	ifstream input;
	input.open("data.txt");
	for (int i = 0; i < 6; i++) {
		input >> s[i].name >> s[i].grade1 >> s[i].grade2 >> s[i].grade3;
		s[i].sum = s[i].grade1 + s[i].grade2 + s[i].grade3;
	}
	cout << "Load Data From: data.txt...\n" << endl;
	cout << "Sort by score1(dec)" << endl;
	for (int i = 1; i < 6; i++) {
		for (int j = 0; j < 6 - i; j++) {
			if (s[j].grade1 < s[j + 1].grade1) {
				student temp = s[j];
				s[j] = s[j + 1];
				s[j + 1] = temp;
			}
		}
	}
	for (int i = 0; i < 6; i++) {
		cout << s[i].name << " " << s[i].grade1 << " " << s[i].grade2 << " " << s[i].grade3 << " " << s[i].sum << endl;	
	}	
	cout << "\n";  
	
	cout << "Sort by sum(dec)" << endl;
	for (int i = 1; i < 6; i++) {
		for (int j = 0; j < 6 - i; j++) {
			if (s[j].sum < s[j + 1].sum) {
				student temp = s[j];
				s[j] = s[j + 1];
				s[j + 1] = temp;
			}
		}
	}
	for (int i = 0; i < 6; i++) {
		cout << s[i].name << " " << s[i].grade1 << " " << s[i].grade2 << " " << s[i].grade3 << " " << s[i].sum << endl;	
	}	  
	cout << "\n";
		
	cout << "Sort by sum(dec)&name(asc)" << endl;
	for (int i = 1; i < 6; i++) {
		for (int j = 0; j < 6 - i; j++) {
			if (s[j].sum < s[j + 1].sum) {
				student temp = s[j];
				s[j] = s[j + 1];
				s[j + 1] = temp;
			}
			if (s[j].sum == s[j + 1].sum) {
				if (s[j].name > s[j + 1].name) {
					student temp = s[j];
					s[j] = s[j + 1];
					s[j + 1] = temp;
				}
 			}
		}
	}
	for (int i = 0; i < 6; i++) {
		cout << s[i].name << " " << s[i].grade1 << " " << s[i].grade2 << " " << s[i].grade3 << " " << s[i].sum << endl;	
	}	  
	return 0;
}
